﻿using System.Collections.Generic;
using System.Web.Http;
using Seed.Data;
using Seed.Model;

namespace Seed.Controllers
{
    public class PeopleController : ApiController
    {
        public IEnumerable<Person> Get()
        {
            return _repository.Query<Person>();
        }

        public Person Get(int id)
        {
            return _repository.Find<Person>(id);
        }

        public Person Post([FromBody]Person value)
        {
            _repository.Add(value);
            _repository.SaveChanges();
            return value;
        }

        public Person Put([FromBody] Person value)
        {
            _repository.Update(value);
            _repository.SaveChanges();
            return value;
        }

        public void Delete(int id)
        {
            var entity = _repository.Find<Person>(id);
            if (entity != null)
            {
                _repository.Remove(entity);
                _repository.SaveChanges();
            }
        }

        protected override void Dispose(bool disposing)
        {
            _repository.Dispose();
            base.Dispose(disposing);
        }

        public PeopleController(IRepository repository)
        {
            _repository = repository;
        }

        private readonly IRepository _repository;

    }
}